package com.example.petstore.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.petstore.common.CommonResponse;
import com.example.petstore.domain.Category;
import com.example.petstore.domain.Product;
import com.example.petstore.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
//    @Resource 是根据 对象名字 区分的， 所以不会重复
//    @Autowired 是根据 类型 区分的，所以同一个类型的 两个对象 会重复
@Service("categoryService")
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public List<String> getCategoryNameList() {
        List<Category> categoryList = categoryMapper.selectList(null);
        List<String> strings = new ArrayList<>();
        for(Category category: categoryList){
            strings.add(category.getName());
        }
//        System.out.println("CategoryService:" + strings);
        return strings;
    }
    public CommonResponse getCategoryList(){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        System.out.println(categoryList);
        return CommonResponse.creatForSuccess(categoryList);

    }

    public CommonResponse getCategoryByCategoryId(String categoryId) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catid", categoryId);
        return CommonResponse.creatForSuccess(categoryMapper.selectOne(queryWrapper));
    }
}
